#include<stdio.h>

#include "buffer.hpp"
#include "log.hpp"

int main()
{
    Log::get_instance()->init(nullptr);

    Log::get_instance()->set_log_level(Log::DEBUG_LOG_LEVEL);

    FILE* fp=fopen("./test_buffer.txt","w+t");
    if(fp==nullptr)
    {
        ERR("open file failed\n");
        return -1;
    }
    int fd=fileno(fp);

    //向文件写入
    const char* content="hello";
    fputs(content,fp);
    //将读写位置指针重置到文件开头
    rewind(fp);
    DBG("content of file:%s",content);

    InBuffer inbuf;
    OutBuffer outbuf;

    //将文件内容读取到buff,再从buff中读取内容
    int r_cnt=inbuf.read_from_fd(fd);
    DBG("read %d bytes from file by InBuffer\n",r_cnt);
    const char* r_data=inbuf.get_from_buff();
    DBG("data get from InBuffer : %s\n",r_data);
    inbuf.clear();

    //向buff中写入内容,再从buff中写入文件
    const char* w_data=" world";
    outbuf.write_to_buff(w_data,strlen(w_data));
    int w_cnt=outbuf.write_to_fd(fd);
    DBG("write %d bytes to file by OutBuffer\n",w_cnt);
    DBG("data written to file by OutBuffer : %s\n",w_data);

    //将文件指针从头开始,重新读取
    rewind(fp);
    r_cnt=inbuf.read_from_fd(fd);
    DBG("read %d bytes from file by InBuffer\n",r_cnt);
    r_data=inbuf.get_from_buff();
    DBG("data get from InBuffer : %s\n",r_data);

    fclose(fp);

    return 0;
}